package org.test.dao.impl;

import org.test.dao.OrderDAO;
import org.test.domain.Client;
import org.test.domain.Order;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDAOImpl implements OrderDAO {

    @Override
    public void create(Order order) throws SQLException {
        EntityManager em = null;
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
            em = emf.createEntityManager();
            em.getTransaction().begin();
            em.persist(order);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null && em.isOpen())
                em.close();
        }
    }

    @Override
    public void update(Long orderId, Order order) throws SQLException {
        Order order1 = null;
        EntityManager em = null;
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
            em = emf.createEntityManager();
            em.getTransaction().begin();
            order = (Order) em.find(Order.class, orderId);
            order1.setOrderDate(order.getOrderDate());
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null && em.isOpen())
                em.close();
        }
    }

    @Override
    public Client getOrdersClient(Long orderId) throws SQLException {
        EntityManager em = null;
        Order order = null;
        Client client = null;
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
            em = emf.createEntityManager();
            em.getTransaction().begin();
            order = (Order) em.find(Order.class, orderId);
            client = (Client) order.getClient();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null && em.isOpen())
                em.close();
        }
        return client;
    }

    @Override
    public Order read(Long orderId) throws SQLException {
        EntityManager em = null;
        Order order = null;
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
            em = emf.createEntityManager();
            em.getTransaction().begin();
            order = (Order) em.find(Order.class, orderId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null && em.isOpen())
                em.close();
        }
        return order;
    }

    @Override
    public List<Order> readAll() throws SQLException {
        EntityManager em = null;
        List<Order> orders = new ArrayList<Order>();
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
            em = emf.createEntityManager();
            em.getTransaction().begin();
            TypedQuery<Order> query = em.createQuery("SELECT order FROM Order order ", Order.class);
            orders = query.getResultList();
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null && em.isOpen())
                em.close();
        }
        return orders;
    }

    @Override
    public void delete(Long orderId) throws SQLException {
        EntityManager em = null;
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
            em = emf.createEntityManager();
            em.getTransaction().begin();
            Order order = read(orderId);
            em.remove(order);
            em.getTransaction().commit();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null && em.isOpen())
                em.close();
        }
    }
}
